using System.IO;
using System.IO.MemoryMappedFiles;
using System.Text;

namespace Catastrophe.Common.FileM
{
	public class FileMemory
	{
		private readonly int _fileCapacity = 1024;

		public int FirstFlagLen = 1;

		private readonly MemoryMappedFile file;

		public FileMemory(string fileName, int capacity = 1024)
		{
			this._fileCapacity = capacity;
			this.file = MemoryMappedFile.CreateOrOpen(fileName, this._fileCapacity);
		}

		public void WriteString(string msg)
		{
			using MemoryMappedViewStream output = this.file.CreateViewStream();
			using BinaryWriter binaryWriter = new BinaryWriter(output);
			binaryWriter.Write(msg);
		}

		public string ReadString()
		{
			using MemoryMappedViewStream input = this.file.CreateViewStream();
			using BinaryReader binaryReader = new BinaryReader(input);
			byte[] array = new byte[this._fileCapacity];
			binaryReader.Read(array, 0, this._fileCapacity);
			if (array.Length != 0)
			{
				string text = Encoding.Default.GetString(array).Replace("\0", "").Trim();
				if (!string.IsNullOrEmpty(text))
				{
					text = text.Remove(0, this.FirstFlagLen);
				}
				return text;
			}
			return "";
		}
	}
}
